﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
           
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“studentsDataSet.StuInfo”中。您可以根据需要移动或删除它。
            

        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load_1(object sender, EventArgs e)
        {
            string wang = "select * from StuInfo";
            var dt = DBhelper.GetDataTable(wang);

            string conString = "server=.;database=Students;uid=sa;pwd=12345678";

            SqlConnection connection = new SqlConnection(conString);

            string sql = "select * from StuInfo";

            SqlDataAdapter adapter = new SqlDataAdapter();

            connection.Open();


          //  DataTable dt = new DataTable();

            adapter.Fill(dt);

            dataGridView1.DataSource = dt;
        }

        private void Form1_Load_2(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“studentsDataSet11.StuInfo”中。您可以根据需要移动或删除它。
            this.stuInfoTableAdapter.Fill(this.studentsDataSet11.StuInfo);
             if(dataGridView1.SelectedRows.Count>0)
            {
                var id = (int)dataGridView1.SelectedRows[0].Cells["StuID"].Value;
                var sql = string.Format("delete  from StuInfo  where StuID={0}", id);
                var res = DBhelper.AddOrUpdate(sql);
                if (res == 1)
                {
                    MessageBox.Show("删除成功", "提示");
                    var wang = "select * from Student";
                    var dt = DBhelper.GetDataTable(wang);
                    dataGridView1.DataSource = dt;
                }
                else {
                    MessageBox.Show("删除失败!", "提示");
                }

            }
            else
            {
                MessageBox.Show("请选择正确的表格!", "提示");
                
            }
        }
        //查找
        private void button4_Click(object sender, EventArgs e)
        {
            var name = textBox1.Text;
            var sql = string.Format("select * from Student where StudentName like '%{0}%'", name);
            var dt = DBhelper.GetDataTable(sql);
            dataGridView1.DataSource = dt;
        }



        public string conString { get; set; }

        private void button2_Click_1(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {

        }

        public object dt { get; set; }
    }
}
